package com.java2401.yangnan.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2401.yangnan.pojo.User;
import com.java2401.yangnan.service.UserService;
import com.java2401.yangnan.util.CommonResult;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserHandler {
    @Resource
    UserService userService;

    @RequestMapping("select")
    public void selectByPrimaryKey(Integer id){
        User user = userService.selectByPrimaryKey(id);
    }

    @RequestMapping("users")
    @ResponseBody
    public PageInfo<User> publicPageInfo(@RequestParam(value = "pn",defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn,10);
        System.out.println("分页查询控制器");
        List<User> users = userService.allUserPage();
        PageInfo<User> pageInfo = new PageInfo<>(users,10);
        return pageInfo;
    }
    @RequestMapping("insert")
    @ResponseBody
    public CommonResult insert(@RequestBody User user){
        System.out.println("99999999"+user);
        User user1 = userService.selectByCard(user.getCard());
        if(user1 != null){
            return CommonResult.failed("身份证号已被注册");
        }
        try {
            // 插入新用户
            int i = userService.insert(user);
            if (i > 0) {
                // 插入成功，返回成功信息（可以根据需要添加更多信息）
                return CommonResult.success("用户添加成功");
            } else {
                // 插入失败
                return CommonResult.failed("用户添加失败，请稍后再试");
            }
        } catch (Exception e) {
            // 捕获并处理可能的异常
            e.printStackTrace(); // 日志记录，实际生产环境中可能需要更详细的日志处理
            return CommonResult.failed("服务器内部错误：" + e.getMessage());
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public CommonResult update(@RequestBody User user){
        User user1 = userService.selectByCard(user.getCard());
        if(user1 != null){
            return CommonResult.failed("身份证号码已存在，请重新输入");
        }
            try {
                Integer i = userService.update(user);
                if(i == 1){
                    return CommonResult.success("用户编辑成功！");
                }else {
                    return CommonResult.failed("用户修改失败");
                }
            }catch (Exception e){
                e.printStackTrace();
                return CommonResult.failed("服务器内部错误：" + e.getMessage());
            }

    }

    @RequestMapping("deleteall")
    @ResponseBody
    public Integer deleteAll(@RequestBody List<Integer> userlist){
        System.out.println("---------" + userlist);
        int i = userService.deleteAll(userlist);
        return 1;
    }

    @RequestMapping("deleteright")
    @ResponseBody
    public Integer deleteByPrimaryKey(@RequestParam("id") Integer id){
        return userService.deleteByPrimaryKey(id);
    }

    @RequestMapping("select1")
    @ResponseBody
    public PageInfo<User> selectLike(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "age" ) Integer age,
            @RequestParam(value = "startTime",required = false,defaultValue = "2030-12-30") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
            @RequestParam(value = "endTime",required = false,defaultValue = "1997-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
            @RequestParam(value = "pn" , required = false , defaultValue = "1") Integer pn
    ){
        PageHelper.startPage(pn,10);
        List<User> userList = userService.selectByDname(name,age, Date.valueOf(startTime),Date.valueOf(endTime));
        System.out.println("------------------"+userList);
        PageInfo<User> pageInfo = new PageInfo<User>(userList,10);
        return pageInfo;
    }

}
